package cn.wolfcode.cloud.redis;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


/**
 * @Description: redis的自动配置类
 * @Author: 伍家钰
 * @Date: 2020/9/7 15:42
 * @Version: 1.0
 */
@Configuration
// 文件中必须要有host自动装配类才会生效
@ConditionalOnProperty(prefix = "redis", value = "host")
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {
    @Bean
    public JedisPool jedisPool(RedisProperties redisProperties) {
        // jedis连接池配置对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大连接数
        jedisPoolConfig.setMaxTotal(redisProperties.getPoolMaxTotal());
        // 设置最大空闲数
        jedisPoolConfig.setMaxIdle(redisProperties.getPoolMaxIdle());
        // 最大等待时间
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getPoolMaxWait());
        /*
         * 参数1:jedis连接池配置
         * 参数2:ip
         * 参数3:端口
         * 参数4:超时时间
         * 参数5:密码
         */
        return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(),
                redisProperties.getTimeout(), redisProperties.getPassword());
    }

    @Bean
    public RedisService redisService() {
        return new RedisService();
    }
}
